package com.bimbimbip.explorer;

import java.io.File;
import java.util.Stack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FolderProperties extends Activity {
	Button ok;
	TextView name, path, parent, info, size, totalSpace;
	int folderCount, fileCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folder_properties);
		ok = (Button) findViewById(R.id.folder_ok);
		ok.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		File file = new File(this.getIntent().getExtras()
				.getString("foldername"));
		Log.d("Extra", this.getIntent().getExtras().getString("foldername"));
		name = (TextView) findViewById(R.id.folderName);
		name.setText(file.getName());
		path = (TextView) findViewById(R.id.folderPath);
		path.setText(file.getPath());
		parent = (TextView) findViewById(R.id.folderParent);
		parent.setText(file.getParent());
		size = (TextView) findViewById(R.id.folderSize);

		size.setText(dirSize(file) + "bytes");
		Log.v("folderSize", dirSize(file) + "bytes");
		totalSpace = (TextView) findViewById(R.id.folderTotal);
		totalSpace.setText(" chua lam ra");
		size = (TextView) findViewById(R.id.folderInfo);
		count(file);

		size.setText(folderCount + " folder " + " : " + fileCount + " file ");

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}

	public long dirSize(File dir) {
		long result = 0;

		Stack<File> dirlist = new Stack<File>();
		dirlist.clear();

		dirlist.push(dir);
		if (dirlist.isEmpty()) {
			result = 0;
			Log.d("Empty", "dirlist.isEmpty()");
		}
		while (!dirlist.isEmpty()) {
			File dirCurrent = dirlist.pop();
			Log.d(" Not Empty", "fsdkfsdklfkds");
			File[] fileList = dirCurrent.listFiles();
			for (int i = 0; i < fileList.length; i++) {

				if (fileList[i].isDirectory())
					dirlist.push(fileList[i]);
				else
					result += fileList[i].length();
			}
		}

		return result;
	}

	public int folderCount(File file) {

		int count = 0;
		File[] listfile = file.listFiles();
		if (listfile != null) {
			for (int i = 0; i < listfile.length; i++) {
				if (!listfile[i].isFile())
					count++;
			}
		}

		return count;

	}

	public int fileCount(File file) {

		int count = 0;
		File[] listfile = file.listFiles();
		if (listfile != null) {
			for (int i = 0; i < listfile.length; i++) {
				if (listfile[i].isFile())
					count++;
			}
		}

		return count;

	}

	public void count(File file) {
		try {
			String child[] = file.list();
			if (child == null)
				return;
			for (String str : child) {
				File childs = new File(file.getAbsoluteFile() + "/" + str);
				if (childs.isFile())
					fileCount++;
				else {
					folderCount++;
					if (childs != null)
						count(childs);
				}

			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
